package com.liziba.trend.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liziba.trend.api.IndexData;
import com.liziba.trend.pojo.FundEquity;
import com.liziba.trend.pojo.FundEquityCrawling;
import com.liziba.trend.pojo.FundSource;
import com.liziba.trend.service.impl.FundEquityServiceImpl;
import com.liziba.trend.service.impl.FundSourceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  基金历年净值数据
 * </p>
 *
 * @author liziba
 * @since 2021-02-04
 */
@RestController
@RequestMapping("/fundEquity")
@CrossOrigin
public class FundEquityController {

    /**
     * 基金代码数据服务
     */
    @Autowired
    private FundSourceServiceImpl fundSourceService;

    /**
     * 基金历年净值服务
     */
    @Autowired
    private FundEquityServiceImpl fundEquityService;


    /**
     * 获取基金净值数据
     * @param code
     * @return
     */
    @GetMapping("/{code}")
    public String get(@PathVariable(value = "code") String code) {
        QueryWrapper<FundEquity> queryWrapper = new QueryWrapper<FundEquity>().select("fund_equity").eq("fund_code", code);
        FundEquity fundEquity = fundEquityService.getOne(queryWrapper, false);
        String fundEquityData = fundEquity.getFundEquity();
        return fundEquityData;
    }


    /**
     * 爬取基金历年净值数据
     * @return
     */
    @GetMapping("/crawlingFundEquityData")
    public String crawlingFundEquityData() {
        // 获取所有基金编码
        List<FundSource> fundSourceList = fundSourceService.list(new QueryWrapper<FundSource>().select("fund_code"));
        String indexDataJsonString = crawlingDataAndSaveFundEquity(fundSourceList);
        return indexDataJsonString;
    }


    /**
     * 爬取指定基金基金历年净值数据
     * @return
     */
    @GetMapping("/crawlingFundEquityData/{code}")
    public String crawlingFundEquityData(@PathVariable(value = "code") String code) {
        // 单个基金代码
        FundSource fundSource = new FundSource();
        fundSource.setFundCode(code);
        List<FundSource> fundSourceList = Arrays.asList(new FundSource[] {fundSource});
        String indexDataJsonString = crawlingDataAndSaveFundEquity(fundSourceList);
        return indexDataJsonString;
    }


    /**
     *  爬取基金数据并且保存基金历史净值
     * @param fundSourceList
     * @return
     */
    private String crawlingDataAndSaveFundEquity(List<FundSource> fundSourceList) {
        // 基金指数json
        String indexDataJsonString = "";
        for (FundSource fundSource : fundSourceList) {
            // 基金编码
            String fundCode = fundSource.getFundCode();
            // 爬取基金净值
            List<FundEquityCrawling> fundEquities = new LinkedList<FundEquityCrawling>();
            for(int i=1; ;i++) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LinkedList<FundEquityCrawling> fundEquitiesPageSize = fundEquityService.crawlingFundEquityInfoMethod(i, 20, "2010-12-1", "2021-2-14", fundCode);
                fundEquities.addAll(fundEquitiesPageSize);
                if(fundEquitiesPageSize.size() < 20) {
                    break;
                }
            }
            List<IndexData> indexData = fundEquityService.convertFundEquityToIndexData(fundEquities);
            if(indexData.size() > 0) {
                indexDataJsonString = JSONObject.toJSONString(indexData);
                // 保存基金历年净值数据JSON
                FundEquity fundEquity = new FundEquity();
                fundEquity.setFundCode(fundCode);
                fundEquity.setFundEquity(indexDataJsonString);
                fundEquityService.save(fundEquity);
            }
        }
        return indexDataJsonString;
    }
}

